﻿@using Newtouch.Infrastructure;
@{
    ViewBag.Title = "PresTemplateDetail";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<div class="gridPanel">
    <table id="gridList"></table>
</div>

<script>
    var mbId = $.request("mbId");
    var cflx = $.request("cflx");
    var brxzCode = $.request("brxzCode");
    var templatePresData;

    var RehabPres = @Html.Raw(((int)EnumCflx.RehabPres).ToString());
    var RegularItemPres = @Html.Raw(((int)EnumCflx.RegularItemPres).ToString());
    var WMPres = @Html.Raw(((int)EnumCflx.WMPres).ToString());
    var TCMPres = @Html.Raw(((int)EnumCflx.TCMPres).ToString());

    $(function () {
        gridList();
    });
    function gridList() {
        if (!mbId) {
            return;
        }
        var $gridList = $("#gridList");

        $.najax({
            url: "/TemplateManage/PresTemplate/SelectPresTemplateDetailByMbId",
            dataType: "json",
            data: { mbId: mbId },
            type: "POST",
            success: function (data) {
                if(data && data.mbmxList && data.mbmxList.length){
                    templatePresData = data;
                    presTemplateGridLoad(data.mbmxList);
                }
                else{
                    templatePresData = null;
                }
            }
        });
    }

    function presTemplateGridLoad(data){
        $("#gridList").newtouchLocalDataGrid({
            height: $(window).height() - 60,
            unwritten: false,
            rownumbers: true,
            multiselect: true,
            sortname:'px',
            sortorder:'asc',
            colModel: [
                { label: 'mxId', name: 'mxId', hidden: true },
                { label: 'px', name: 'px', hidden: true },
                { label: '组号', name: 'zh', width: 60, align: 'left', hidden: cflx == WMPres ? null : true },
                { label: '项目名称', name: 'xmmc', width: 120, align: 'left', hidden: (cflx == RehabPres || cflx == RegularItemPres) ? null : true },
                { label: '药品名称', name: 'ypmc', width: 120, align: 'left', hidden: cflx == WMPres ? null : cflx == TCMPres ? null : true },
                { label: '规格', name: 'ypgg', width: 100, align: 'left', hidden: cflx == WMPres ? null : cflx == TCMPres ? null : true },
                { label: '剂量（单位）', name: 'mcjl', width: 90, align: 'left', hidden: cflx == WMPres ? null : cflx == TCMPres ? null : true },
                { label: '用法', name: 'yfmc', width: 70, align: 'left', hidden: (cflx == RehabPres || cflx == RegularItemPres) ? true : null},
                { label: '单价', name: 'dj', width: 60, align: 'left' , hidden: cflx == TCMPres ? true : null, formatter: "number"
                    , formatoptions: { decimalPlaces: 2, defaultValue: '0.00' }},
                { label: '每次治疗量', name: 'mczll', width: 70, align: 'left', hidden: (cflx == RehabPres || cflx == RegularItemPres) ? null : true },
                { label: 'pcCode', name: 'pcCode', hidden: true },
                { label: '频次', name: 'pcmc', width: 70, align: 'left', hidden: cflx == TCMPres ? true : null},
                { label: '数量', name: 'sl', width: 50, align: 'left' , hidden: cflx == TCMPres ? true : null},
                { label: '部位', name: 'bw', width: 160, align: 'left', hidden: (cflx == RehabPres || cflx == RegularItemPres) ? null : true  },
                { label: '总量（单位）', name: 'zl', width: 100, align: 'left', hidden: (cflx == RehabPres || cflx == RegularItemPres) ? null : true, formatter: function (val, a, b) {
                    if((cflx == RehabPres || cflx == RegularItemPres)){
                        return (b.mczll * b.sl) + '(' + b.dw + ')';
                    }
                    else{
                        return"";
                    }
                }
                },
                { label: '执行科室', name: 'zxks', hidden: true },
                { label: '执行科室名称', name: 'zxksmc', width: 80, hidden: (cflx == RehabPres) ? null : true },
                { label: '是否医保', name: 'sfyb', width:80},
            ], onSelectRow: function (rowid, status) {
                if (status === true) {
                    var data = $("#gridList").getRowData(rowid);
                    if (brxzCode!=""&&'@ViewBag.ControlbrxzCode'.indexOf(brxzCode) > -1 && data.sfyb == "否") {
                        $.modalAlert("该患者为医保患者，不可选择自费项目【" + (data.xmmc == "" ? data.ypmc : data.xmmc) + "】", "warning");
                        $("#gridList").jqGrid('setSelection', rowid, false); //设置jqGrid选中
                        return false;
                    }
                }
            },
            viewrecords: true,
        }, data.reverse());
    }

    function getSelectRowData() {
        var selectRowData = [];
        var rowIds = $("#gridList").jqGrid('getDataIDs');
        for (var k = 0; k < rowIds.length; k++) {
            var curChk = $("#" + rowIds[k]).find(":checkbox");
            if (curChk[0].checked) {
                var rowData = $("#gridList").jqGrid('getRowData', rowIds[k]);
                selectRowData.push(rowData);
            }
        }
        return selectRowData;
    }

    function submitForm(callback) {
        if(!templatePresData || !templatePresData.mbmxList){
            return;
        }
        var data = getSelectRowData();
        if(!data || data.length==0){
            $.modalAlert("请选择明细进行复制", 'warning');
            return;
        }
        var newMxList = new Array();
        $.each(data, function () {
            var thisMxId = this.mxId;
            var mtachddd = $.jsonWhere(templatePresData.mbmxList, function(v){
                return v.mxId == thisMxId;
            });
            if(mtachddd && mtachddd.length == 1){
                newMxList.push(mtachddd[0]);
            }
        });
        templatePresData.mbmxList = newMxList;
        callback(templatePresData);
    }
</script>

